package com.atguigu.aiproject.consumer;

import com.atguigu.aiproject.config.RabbitMQConfig;
import com.atguigu.aiproject.vo.MailMessageVO;
import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MailSenderConsumer {
    @Resource
    private JavaMailSender mailSender;
    private final String from;


    public MailSenderConsumer( @Value("${spring.mail.username}") String from) {
        this.from = from;
    }

    @RabbitListener(queues = RabbitMQConfig.MAIL_QUEUE_NAME)
    public void processMailMessage(MailMessageVO mailMessageVO) {
        try {
            log.info("尝试将邮件发送至: {}", mailMessageVO.getToEmail());
            sendHtmlMail(mailMessageVO);
        } catch (Exception e) {
            log.error("邮件发送失败: {}", mailMessageVO.getToEmail(), e);
            // 这里可以添加重试逻辑或者将失败消息存入死信队列
            throw new AmqpRejectAndDontRequeueException("邮件发送失败");
        }
    }

    /**
     * 发送单封邮件
     */
    public void sendSingleMail(MailMessageVO mailMessageVO) {
        try {
            String personalizedContent = "尊敬的 " + mailMessageVO.getNicknames() + " 先生/女士：" + mailMessageVO.getContent();

            SimpleMailMessage mail = new SimpleMailMessage();
            mail.setFrom(from);
            mail.setTo(mailMessageVO.getToEmail());
            mail.setSubject(mailMessageVO.getSubject());
            mail.setText(personalizedContent);

            mailSender.send(mail);
            log.info("邮件发送成功: {} -> {}", from, mailMessageVO.getToEmail());

        } catch (Exception e) {
            log.error("邮件发送失败: {}", mailMessageVO.getToEmail(), e);
            throw new RuntimeException("邮件发送失败", e);
        }
    }

    /**
     * 发送HTML格式邮件
     */
    public void sendHtmlMail(MailMessageVO mailMessageVO) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            String htmlContent = buildHtmlContent(mailMessageVO);

            helper.setFrom(from);
            helper.setTo(mailMessageVO.getToEmail());
            helper.setSubject(mailMessageVO.getSubject());
            helper.setText(htmlContent, true);

            mailSender.send(message);
            log.info("HTML邮件发送成功: {} -> {}", from, mailMessageVO.getToEmail());

        } catch (Exception e) {
            log.error("HTML邮件发送失败: {}", mailMessageVO.getToEmail(), e);
            throw new RuntimeException("HTML邮件发送失败", e);
        }
    }

    private String buildHtmlContent(MailMessageVO mailMessageVO) {
        return "<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<meta charset=\"UTF-8\">" +
                "<title>" + mailMessageVO.getSubject() + "</title>" +
                "</head>" +
                "<body>" +
                "<div style=\"max-width: 600px; margin: 0 auto; padding: 20px;\">" +
                "<h2 style=\"color: #333;\">" + mailMessageVO.getSubject() + "</h2>" +
                "<p>尊敬的 <strong>" + mailMessageVO.getNicknames() + "</strong> 先生/女士：</p>" +
                "<div style=\"background: #f5f5f5; padding: 15px; border-radius: 5px;\">" +
                "<p>" + mailMessageVO.getContent() + "</p>" +
                "</div>" +
                "<hr>" +
                "<p style=\"color: #666; font-size: 12px;\">此邮件由AI发送，请勿回复</p>" +
                "</div>" +
                "</body>" +
                "</html>";
    }
}
